﻿using System;
using System.Diagnostics;
using System.Globalization;

namespace Common.Utilities.TextMatch
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class SoundexConverter : ExSoundexConverter
    {

        /// <summary>
        /// 
        /// </summary>
        public SoundexConverter() : base() { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="resultLength"></param>
        public SoundexConverter(int resultLength) : base(resultLength) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="miraCode"></param>
        public SoundexConverter(bool miraCode) : base(miraCode) { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="resultLength"></param>
        /// <param name="miraCode"></param>
        public SoundexConverter(int resultLength, bool miraCode) : base(resultLength, miraCode) { }

        //Encodes a value using the Soundex algorithm.
        /// <summary>
        /// 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public override string Convert(string value)
        {
            value = StringUtil.RemoveNonAlphaNumericCharacters(value);
            //Ensure the parameter is valid
            Debug.Assert(value != null, "Unexpected null value.", "The SoundexConverter.Convert(String) method requires a value.");

            if (value == null)
                throw new ArgumentNullException("value");

            //Validate input
            if (!(new AlphabeticValidator().Validate(value)))
            {
                return string.Empty;
                //throw new ArgumentOutOfRangeException("inputValue", value,
                //                                      NamespaceUtility.Resource["Error.InvalidPhoneticInput"]);
            }
            else
            {
                value = value.ToUpper(CultureInfo.InvariantCulture);

                if (value.Length > 1)
                    return (value[0] + base.DoSoundex(value, this.ResultLength - 1, this.MiraCode, false));
                else
                    return value[0].ToString();
            }
        }

    }	
}
